angular.module('brandadd', [])
.controller('brandaddCtrl', ['$scope', 'httpService', 'alertmsg', '$stateParams', '$timeout', function($scope, httpService, alertmsg, $stateParams, $timeout) {
  $scope.step = 1;


  // 获取品牌下的部门 - 用于下拉选择
  $scope.getDepartment = function() {
    httpService.query('web/companies/' + user.company_id + '/fetch_departments', { limit: 999 }).then(function(res) {
      if(res.code == 200) {
        $scope.departments = res.departments;
      }
    });
  };

  if($stateParams.type) {
    $scope.step = $stateParams.type;
    $scope.getDepartment();
  }
  
  var copy_depart;
  if($stateParams.department) {
    $scope.getDepartment();
    $scope.create_role_depart = JSON.parse($stateParams.department);
    copy_depart = angular.copy(JSON.parse($stateParams.department));
    $scope.step = 2;
  } else {
    // 创建角色
    $scope.create_role_depart = {};
  }


  // 添加创建的部门数组
  $scope.create_departments = [];

  // 预添加的部门
  $scope.depart = {};
  $scope.adddepart = function(depart) {
    var idx = $scope.create_departments.indexOf(depart);
    if(depart.name) {
      if(idx == -1) {
        $scope.create_departments.push(depart);
        $scope.depart = {};
      } else {
        alertmsg('该部门已经存在!');
      }
    } else {
      alertmsg('请输入部门名称!');
    }
  };

  // 删除预添加的部门
  $scope.deletedepart = function(index) {
    $scope.create_departments.splice(index, 1);
  };

  // 确认添加部门
  $scope.createDepartment = function() {
    if($scope.create_departments.length > 0) {
      httpService.query('web/companies/' + user.company_id + '/create_departments', { names: $scope.create_departments}, 'post').then(function(res) {
        if(res.code == 200) {
          $scope.getDepartment();
          $scope.step = 2;
          $scope.create_departments = [];
          $scope.create_role_depart.department_id = res.departments[0].id;
          alertmsg('添加部门成功！');
        } else {
          alertmsg(res.msg);
        }
      });
    } else {
      alertmsg('请添加部门后在确认！');
    }
  };


  // 添加角色
  $scope.createRoles = function() {
    var url = 'web/companies/' + user.company_id + '/create_roles';
    if($scope.create_role_depart.id) {
      url = 'web/companies/' + user.company_id + '/edit_roles';
    }
    var params = {};
    if(copy_depart) {
      params.role_id =  $scope.create_role_depart.id.toString();
      if($scope.create_role_depart.department_id != copy_depart.department_id) {
        params.department_id = $scope.create_role_depart.department_id;
      }
      if($scope.create_role_depart.name != copy_depart.name) {
        params.name = $scope.create_role_depart.name;
      }
      if($scope.create_role_depart.rules != copy_depart.rules) {
        params.rules = $scope.create_role_depart.rules;
      }
    } else {
      params = $scope.create_role_depart;
    }
      
    $timeout(function() {
      httpService.query(url, params, 'post').then(function(res) {
        if(res.code == 200) {
          $scope.create_role_depart = {};
          window.history.go(-1);
          if($scope.create_role_depart.id) {
            alertmsg('修改角色成功!');
          } else {
            alertmsg('添加角色成功!');
          }
        } else {
          alertmsg(res.msg);
        }
      });
    }, 200);
  };

}]);